package cn.datawin.bms.modules.customer.controller;

import cn.datawin.bms.customer.bo.CstmCustomerBo;
import cn.datawin.bms.customer.bo.CstmCustomerConfigBo;
import cn.datawin.bms.customer.entity.CstmCustomerEntity;
import cn.datawin.bms.customer.entity.CstmCustomerProviderChannelEntity;
import cn.datawin.bms.customer.vo.CstmCustomerConfigVo;
import cn.datawin.bms.modules.customer.service.CstmCustomerCmppConfigService;
import cn.datawin.bms.modules.customer.service.CstmCustomerProviderChannelService;
import cn.datawin.bms.modules.customer.service.CstmCustomerService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 下游客户
 *
 * @author datawin-sms
 * @date 2025-06-17 14:46:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/customer/customer" )
@Tag(description = "cstmCustomer" , name = "下游客户管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CstmCustomerController {

    private final CstmCustomerService cstmCustomerService;
    private final CstmCustomerProviderChannelService cstmCustomerProviderChannelService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param cstmCustomer 下游客户
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('customer_customer_view')" )
    public R getCstmCustomerPage(@ParameterObject Page<CstmCustomerEntity> page, @ParameterObject CstmCustomerEntity cstmCustomer) {

		return R.ok(cstmCustomerService.customPage(page,cstmCustomer));
    }


    /**
     * 通过id查询下游客户
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('customer_customer_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(cstmCustomerService.getInfo(id));
    }

	/**
	 * 通过id查询下游客户的参数
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询下游客户的参数" , description = "通过id查询下游客户的参数" )
	@GetMapping("/getConfig/{id}" )
	@PreAuthorize("@pms.hasPermission('customer_customer_view')" )
	public R getConfig(@PathVariable("id" ) Long id) {
		return R.ok(cstmCustomerService.getConfigByCustomerId(id));
	}


	/**
	 * 更新id查询下游客户的参数
	 * @param bo 参数数据
	 * @return R
	 */
	@Operation(summary = "更新id查询下游客户的参数" , description = "更新id查询下游客户的参数" )
	@PostMapping("/updateConfig" )
	@PreAuthorize("@pms.hasPermission('customer_customer_edit')" )
	public R updateConfig(@RequestBody CstmCustomerConfigBo bo ) {
		return R.ok(cstmCustomerService.updateConfigByCustomerId(bo));
	}

    /**
     * 新增下游客户
     * @param bo 下游客户
     * @return R
     */
    @Operation(summary = "新增下游客户" , description = "新增下游客户" )
    @SysLog("新增下游客户" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('customer_customer_add')" )
    public R save(@RequestBody CstmCustomerBo bo) {
        return R.ok(cstmCustomerService.insertByBo(bo));
    }


	/**
	 * 构建新下游客户账号
	 * @return R
	 */
	@Operation(summary = "构建新下游客户账号" , description = "构建新下游客户账号" )
	@SysLog("构建新下游客户公司" )
	@GetMapping("/new")
	@PreAuthorize("@pms.hasPermission('customer_customer_add')" )
	public R newCstmCustomer() {
		return R.ok(cstmCustomerService.newCstmCustomer());
	}


    /**
     * 修改下游客户
     * @param bo 下游客户
     * @return R
     */
    @Operation(summary = "修改下游客户" , description = "修改下游客户" )
    @SysLog("修改下游客户" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('customer_customer_edit')" )
    public R updateById(@RequestBody  CstmCustomerBo bo) {
        return R.ok(cstmCustomerService.updateByBo(bo));
    }

    /**
     * 修改下游客户状态
     * @param cstmCustomer 下游客户
     * @return R
     */
    @Operation(summary = "修改下游客户状态" , description = "修改下游客户状态" )
    @SysLog("修改下游客户状态" )
    @PutMapping("updateStatus")
    @PreAuthorize("@pms.hasPermission('customer_customer_updateStatus')" )
    public R updateStatus(@RequestBody CstmCustomerEntity cstmCustomer) {
        return R.ok(cstmCustomerService.updateStatus(cstmCustomer.getId(),cstmCustomer.getStatus()));
    }

    /**
     * 通过id删除下游客户
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除下游客户" , description = "通过id删除下游客户" )
    @SysLog("通过id删除下游客户" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('customer_customer_del')" )
    public R removeById(@RequestBody Long[] ids) {
		return R.ok(cstmCustomerService.removeBatch(ids));
    }


    /**
     * 导出excel 表格
     * @param cstmCustomer 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('customer_customer_export')" )
    public List<CstmCustomerEntity> export(CstmCustomerEntity cstmCustomer,Long[] ids) {
        return cstmCustomerService.list(Wrappers.lambdaQuery(cstmCustomer).in(ArrayUtil.isNotEmpty(ids), CstmCustomerEntity::getId, ids));
    }



	/**
	 * 获取下游客户绑定的上游供应商通道
	 * @param customerId 下游客户ID
	 * @return R
	 */
	@Operation(summary = "绑定下游客户上游供应商通道" , description = "绑定下游客户上游供应商通道" )
	@SysLog("绑定下游客户上游供应商通道" )
	@GetMapping("/getProviderChannels")
	@PreAuthorize("@pms.hasPermission('customer_customer_bindChannel')" )
	public R getProviderChannels(Long customerId) {
		return R.ok(cstmCustomerProviderChannelService.getProviderChannels(customerId));
	}

	/**
	 * 绑定下游客户上游供应商通道
	 * @param channelList 通道列表
	 * @return R
	 */
	@Operation(summary = "绑定下游客户上游供应商通道" , description = "绑定下游客户上游供应商通道" )
	@SysLog("绑定下游客户上游供应商通道" )
	@PostMapping("/bindProviderChannel")
	@PreAuthorize("@pms.hasPermission('customer_customer_bindChannel')" )
	public R bindProviderChannel(@RequestBody List<CstmCustomerProviderChannelEntity> channelList) {
		return R.ok(cstmCustomerProviderChannelService.bindProviderChannel(channelList));
	}

}
